

# 自定义过滤器
from flask import current_app
from flask import session,g

from info.models import User


def index_filrst(index):
    if index == 0:
        return 'first'
    elif index == 1:
        return 'second'
    elif index == 2:
        return 'third'
    else:
        return ''

# 装饰其:函数嵌套，本质是函数嵌套，不修改原来代码，添加新的功能
def login_required(f):
    def wrapper(*args,**kwargs):
        user_id = session.get('user_id')
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        # flask内置对象，和session  reqsuest一样
        # 用来临时存储数据
        g.user = user
        return f(*args,**kwargs)
    #
    wrapper.__name__ = f.__name__

    return wrapper